package cn.tedu.sp02.item.controller;

import cn.tdeu.sp01.pojo.Item;
import cn.tdeu.sp01.service.ItemService;
import cn.tdeu.sp01.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Controller
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;
    @Value("${server.port}")
    private Integer port;
    @RequestMapping("/{orderId}")
   @ResponseBody
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {

        //模拟超时失败
        if (Math.random()<0.9){
            long t=new Random().nextInt(5000);
            System.out.println("延迟: "+t);
            Thread.sleep(t);
        }

        List<Item> items=itemService.getItems(orderId);
        return JsonResult.ok().data(items).msg("port="+port);
    }

    @PostMapping("/decreaseNumber")
    @ResponseBody
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        itemService.decteaseNumber(items);
        return JsonResult.ok().msg("减少商品库存减少");
    }
}
